<template>
	<view class="container" @tap="cancle()">
		<view class="banner">
			<swiper class="swiper" :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
				:duration="duration" easing-function="easeOutCubic" circular>
				<swiper-item v-for="(item,i) in bgUrl" :key='i'>
					<view class="swiper-item uni-bg-red">
						<image :src="item" mode="aspectFill" class="bg" show-menu-by-longpress></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="content">
			<view class="info" v-if="isLogin">
				<view class="integral_section">
					<view class="top">
						<text class="title">我的余额</text>
						<text class="value">{{ isLogin ? member.balance : 0 }}</text>
					</view>
					<view class="bottom" @tap="integrals">
						进入积分商城
						<view class="iconfont iconarrow-right"></view>
					</view>
				</view>
				<view class="qrcode_section" @tap="memberCode">
					<image src="/static/images/index/qrcode.png"></image>
					<text class="title">会员码</text>
				</view>
			</view>
			<view class="info" v-else>
				<view class="integral_section">
					<view class="top">
						<text class="title">尊敬的用户</text>
						<text class="value"><strong></strong></text>
					</view>
					<view class="bottom" @tap="integrals">
						<text>会员可享受【闲时优惠】等多项权益</text>
					</view>
				</view>
				<view class="qrcode_section">
					<button type="primary" size="default" class="login-btn" @tap="getPhoneNumber('')" v-if="phoneNumber">用户登录</button>
					<button type="primary" size="default" class="login-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" v-else>用户登录</button>
				</view>
			</view>

			<view class="member-news">
				<view class="entrancetwo" v-for="(item, i) in menuAction" :key="i">
					<view class="item" @tap="goUrl(item)">
						<view class="icoms">
							<image :src="item.icon" mode="aspectFill"></image>
						</view>

						<view class="title">{{item.name}}</view>

					</view>
				</view>
			</view>
			<!-- <view class="buy-type" @tap="takein">
				<button type="primary" size="default" class="order-btn">开始点餐</button>
			</view> -->

			<view class="top-goods" v-for="(item, i) in sildeshow" :key="i" @tap="goGoods(item.product_id)">
				<image :src="item.image" mode="aspectFill"></image>
			</view>
		</view>
		<!-- 加载弹窗 -->
		<view class="shade" v-if="showShade && (!member.svip && !member.vip)">
			<image src="/static/images/vip.png" mode="" class="vip"></image>
			<view class="cancle" @tap="cancle()"></view>
			<view class="govip" @tap="goVip()"></view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapActions
	} from 'vuex'

	export default {
		data() {
			return {
				bgUrl: [],
				menuAction: [],
				indicatorDots: false,
				autoplay: true,
				interval: 6000,
				duration: 500,
				showShade: true,
				appId: '',
				isQrcode: 0,
				sildeshow: [],
				phoneNumber:''
			}
		},
		computed: {
			...mapState(['member', 'address']),
			...mapGetters(['isLogin'])
		},
		onLoad(e) {
			console.log('e=>', e)
			// 如果是扫码进来
			if (e.hasOwnProperty('scene')) {
				this.isQrcode = 1
				uni.setStorageSync('admin_id', e.scene)
			} else {
				uni.setStorageSync('admin_id', e.admin_name)
				uni.setStorageSync('token', e.token)
				console.log('e1=>', e)
				// 区分多个小程序
				// this.appId = e.appId;
				// if (!uni.getStorageSync('admin_id')) {
				// 	return uni.redirectTo({
				// 		url:'/pages/restaurant/look-over'
				// 	})
				// }
			}
		},
		onShow() {
			this.getLogin()
			uni.showTabBar({})
			// 获取系统配置
			this.getSysConfig()
			// 获取轮播商品
			this.getShowGoods()
			this.phoneNumber = uni.getStorageSync('getPhoneNumber')
			// if (!this.phoneNumber) {
			// 	uni.showLoading({
			// 		title:'授权登录中',
			// 		mask:true
			// 	})
			// 	this.getUserInfo()
			// }
		},
		onShareAppMessage(res) {
			// console.log(this.shareGood)
			return {
				title: '麦乐士',
				path: '/pages/index/index',
				imageUrl: this.bgUrl[0]
			}
		},
		onShareTimeline(res) {
			return {
				title: '麦乐士',
				path: '/pages/index/index',
				imageUrl: this.bgUrl[0]
			}
		},
		methods: {
			...mapActions(['setLogin', 'getLogin', 'getUserInfo']),
			takein() {
				this.$store.commit('SET_ORDER_TYPE', 'takein')
				uni.switchTab({
					url: '/pages/menu/menu'
				})
			},
			takeout() {
				if (!this.isLogin) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return
				}
				if (this.address.street) {
					uni.switchTab({
						url: '/pages/menu/menu'
					})
				} else {
					uni.navigateTo({
						url: "/pages/address/address?is_choose=true"
					})
				}

			},
			integrals() {
				uni.navigateTo({
					url: '/pages/integrals/integrals'
				})
			},
			packages() {
				uni.navigateTo({
					url: '/pages/packages/index'
				})
			},
			memberCode() {
				uni.navigateTo({
					url: '/pages/mine/member-code'
				})
			},
			goCoupons() {
				uni.navigateTo({
					url: '/pages/coupons/coupons?go=menu'
				})
			},
			invite() {
				uni.navigateTo({
					url: '/pages/activities/invite'
				})
			},
			noAction() {
				uni.showToast({
					icon: 'none',
					title: '未开通！'
				})
			},
			goVip() {
				this.showShade = false
				uni.navigateTo({
					url: '/pages/mine/vip'
				})
			},
			cancle() {
				this.showShade = false
			},
			checkVersion(data) {
				var that = this
				const updateManager = uni.getUpdateManager()
				let version = uni.getStorageSync('version')
				if (data.version > version) {
					updateManager.onCheckForUpdate((res) => {
						// 请求完新版本信息的回调
						if (res.hasUpdate) {
							uni.showModal({
								title: '更新提示',
								content: '检测到新版本，是否下载新版本并重启小程序？',
								success: (res) => {
									if (res.confirm) {
										//2. 用户确定下载更新小程序，小程序下载及更新静默进行
										that.downLoadUpdate(updateManager, data.version)
									} else if (res.cancel) {
										//用户点击取消按钮的处理，如果需要强制更新，则给出二次弹窗，如果不需要，则这里的代码都可以删掉了
										uni.showModal({
											title: '温馨提示',
											content: '本次版本更新涉及到新的功能添加，新版本体验更好',
											showCancel: false, //隐藏取消按钮
											confirmText: "确定更新", //只保留确定更新按钮
											success: (res) => {
												if (res.confirm) {
													//下载新版本，并重新应用
													that.downLoadUpdate(updateManager, data
														.version)
												}
											}
										})
									}
								}
							})
						}
					})
				}
			},
			// 下载小程序新版本并重启应用
			downLoadUpdate(updateManager, version) {
				uni.showLoading();
				//静默下载更新小程序新版本
				updateManager.onUpdateReady(() => {
					uni.hideLoading()
					// 写入当前版本
					uni.setStorageSync('version', version)
					//新的版本已经下载好，调用 applyUpdate 应用新版本并重启
					updateManager.applyUpdate()
				})
				updateManager.onUpdateFailed(() => {
					// 新的版本下载失败
					uni.showModal({
						title: '已经有新版本了',
						content: '新版本已经上线，请您删除当前小程序，重新搜索打开',
					})
				})
			},
			getSysConfig() {
				this.$http.get('sys/config', {
					params: {
						'wx_min_app_appid': this.appId,
						'isQrcode': this.isQrcode
					}
				}).then(res => {
					if (res.data.code == 1) {
						this.bgUrl = res.data.data.bg_img
						this.menuAction = res.data.data.menuAction
						if (!res.data.data.is_show) {
							this.showShade = res.data.data.is_show
						}
						// 非扫码并且没有admin_id
						if (!this.isQrcode && !uni.getStorageSync('admin_id')) {
							uni.setStorageSync('admin_id', res.data.data.admin_id)
						}
						// 判断版本
						this.checkVersion(res.data.data)
					}
				}).catch(err => {

				})
			},
			getShowGoods() {
				this.$http.post('/sildeshow', {
					type: 2
				}).then(res => {
					// console.log(res)
					this.sildeshow = res.data.data
				}).catch(err => {

				})
			},
			goGoods(good) {
				good.number = 1
				let path = {
					item: good
				};

				uni.setStorageSync('searchGoods', path)
				uni.switchTab({
					url: '/pages/menu/menu'
				})
			},
			goUrl(item) {
				if (item.is_login && !this.isLogin) {
					return uni.showToast({
						icon:'none',
						title:'请先登录'
					})
				}
				this.showShade = false
				uni.navigateTo({
					url: item.url
				})
			},
			getPhoneNumber(e){
				this.setLogin(e)
				setTimeout( () => {
					uni.hideLoading()
					uni.showToast({
						icon:'none',
						title:'登录成功'
					})
				}, 500)
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifdef H5 */
	page {
		height: auto;
		min-height: 100%;
	}

	/* #endif */

	.container {
		background-color: #ffffff;
	}

	.banner {
		position: relative;
		width: 100%;

		.swiper {
			height: 550rpx;
		}

		.bg {
			width: 100%;
			height: 550rpx;
			border-bottom-left-radius: 16rpx;
			border-bottom-right-radius: 16rpx;
		}

		.intro {
			position: absolute;
			top: calc(50rpx + var(--status-bar-height));
			left: 40rpx;
			color: #FFFFFF;
			display: flex;
			flex-direction: column;

			.greet {
				font-size: $font-size-lg;
				margin-bottom: 10rpx;
			}

			.note {
				font-size: $font-size-sm;
			}
		}
	}

	.content {
		padding: 0 30rpx;
	}

	.top-goods {
		margin-top: 30rpx;
		width: 100%;
		height: 300rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 30rpx;
		}
	}

	.buy-type {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 30rpx;

		.order-btn {
			width: 100%;
			border-radius: 50rem !important;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 10rpx 0;
		}

		.entrance {
			width: 49%;
			height: 330rpx;
			position: relative;
			margin-bottom: 30rpx;
			border-radius: 10rpx;
			background-color: #ffffff;
			box-shadow: $box-shadow;
			display: flex;
			align-items: center;
			justify-content: center;

			.item {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				&:nth-child(1):after {
					position: absolute;
					width: 1rpx;
					background-color: #ddd;
					right: 0;
					height: 100%;
					transform: scaleX(0.5) scaleY(0.8);
				}

				.icon {
					width: 260rpx;
					height: 200rpx;
					position: absolute;
					bottom: 0rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.icon-two {
					bottom: -10rpx;
				}

				.title {
					font-size: 30rpx;
					color: $text-color-base;
					font-weight: 600;
					position: absolute;
					left: 50rpx;
					top: 50rpx;
				}

				.desc {
					position: absolute;
					left: 50rpx;
					top: 105rpx;
					color: #878787;
				}
			}
		}

		.entrance-nop {
			padding: 14rpx;
			background-color: #F1F8FA;
		}
	}

	.entrancetwo {
		background-color: #ffffff;
		box-shadow: 0 20rpx 20rpx -20rpx rgba(51, 51, 51, 0.1);
		display: flex;
		height: 220rpx;

		.item {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			&:nth-child(1):after {
				position: absolute;
				width: 1rpx;
				background-color: #ddd;
				right: 0;
				height: 100%;
				transform: scaleX(0.5) scaleY(0.8);
			}

			.icoms {
				width: 119rpx;
				height: 120rpx;

				image {
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}

			.title {
				font-size: 30rpx;
				color: $text-color-base;
				font-weight: 600;
			}
		}
	}

	.info {
		position: relative;
		margin-bottom: 30rpx;
		border-radius: 10rpx;
		background-color: #ffffff;
		box-shadow: $box-shadow;
		padding: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: -30rpx;

		.integral_section {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;

			.top {
				margin-bottom: 20rpx;

				.title {
					font-weight: 600;
					color: $text-color-base;
					font-size: $font-size-base;
					margin-right: 10rpx;
				}

				.value {
					font-size: 44rpx;
					font-weight: bold;
				}
			}

			.bottom {
				font-size: $font-size-sm;
				color: $text-color-assist;
				display: flex;
				align-items: center;
			}
		}

		.qrcode_section {
			color: $color-primary;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: $font-size-sm;

			.title {
				color: $text-color-base;
				font-size: $font-size-base;
			}

			image {
				width: 40rpx;
				height: 40rpx;
				margin-bottom: 10rpx;
			}

			.login-btn {
				border-radius: 20rpx;
			}
		}
	}

	.navigators {
		width: 100%;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
		background-color: #ffffff;
		box-shadow: $box-shadow;
		padding: 20rpx;
		display: flex;
		align-items: stretch;

		.left {
			width: 340rpx;
			margin-right: 20rpx;
			display: flex;
			padding: 0 20rpx;
			flex-direction: column;
			font-size: $font-size-sm;
			color: $text-color-base;
			background-color: #F2F2E6;

			.grid {
				height: 50%;
				display: flex;
			}
		}

		.right {
			width: 290rpx;
			display: flex;
			flex-direction: column;

			.tea-activity,
			.member-gifts {
				width: 100%;
				display: flex;
				padding: 20rpx;
				font-size: $font-size-sm;
				color: $text-color-base;
				align-items: center;
				position: relative;
			}

			.tea-activity {
				background-color: #FDF3F2;
				margin-bottom: 20rpx;
			}

			.member-gifts {
				background-color: #FCF6D4;
			}

			.right-img {
				flex: 1;
				position: relative;
				margin-left: 20rpx;
				margin-right: -20rpx;
				margin-bottom: -20rpx;
				display: flex;
				align-items: flex-end;

				image {
					width: 100%;
				}
			}
		}

		.mark-img {
			width: 30rpx;
			height: 30rpx;
			margin-right: 10rpx;
		}

		.yzclh-img {
			height: 122.96rpx;
			width: 214.86rpx;
		}
	}

	.member-news {
		display: flex;
		justify-content: space-between;

		.item {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			position: relative;

			&:nth-child(1):after {
				position: absolute;
				width: 1rpx;
				background-color: #ddd;
				right: 0;
				height: 100%;
				transform: scaleX(0.5) scaleY(0.8);
			}

			.icon {
				width: 84rpx;
				height: 84rpx;
				position: absolute;
				right: 10rpx;
				top: 68rpx;
			}

			.title {
				font-size: 26rpx;
				color: #5A5B5C;
				font-weight: 600;
				margin-top: 20rpx;
			}
		}
	}

	.shade {
		width: 100%;
		height: 100%;
		background: #eee;
		position: fixed;
		top: 0;
		background: rgba(0, 0, 0, .5);

		.vip {
			margin: 10% auto;
			display: block;
			height: 100%;
		}

		.cancle {
			width: 80rpx;
			height: 80rpx;
			// background: red;
			position: fixed;
			top: 43%;
			left: 69%;
		}

		.govip {
			width: 300rpx;
			height: 100rpx;
			position: fixed;
			top: 69%;
			left: 30%;
			// background: red;
		}
	}
</style>